package com.atstudy.headline.web.admin.controller.contents;

import com.atstudy.headline.entity.Categories;
import com.atstudy.headline.entity.Contents;
import com.atstudy.headline.enums.ResultCodeEnum;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.CategoriesService;
import com.atstudy.headline.web.admin.service.ContentsService;
import com.atstudy.headline.web.admin.vo.contents.CommentsInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/contents")
public class contentsController {

    @Autowired
    private ContentsService contentsService;

    @Autowired
    private CategoriesService categoriesService;

    @GetMapping("/page")
    public Result<Map<String, Object>> pageInfoByComments(@RequestParam Integer current, @RequestParam Integer size, CommentsInfoQueryVo queryVo){
        Page<Contents> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        QueryWrapper<Contents> wrapper = new QueryWrapper<>();
        if(queryVo!=null){
            wrapper.like("title",queryVo.getTitle());
            wrapper.eq("category_id",queryVo.getCategoryId());
            wrapper.between("create_time",queryVo.getStartTime(),queryVo.getEndTime());
        }
        IPage<Contents> commentsIPage = contentsService.pageInfoByContents(page, queryVo);
        List<Categories> list = categoriesService.list();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("contentsPage", commentsIPage);
        resultMap.put("categoriesList", list);
        return Result.ok(resultMap);

    }



    /**
     * 修改新闻信息
     * @param
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Contents contents){
        contentsService.saveOrUpdate(contents);
        return Result.ok();
    }

    /**
     * 删除新闻信息
     * @param
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer contentId){
        contentsService.removeById(contentId);
        return Result.ok();
    }

    /**
     * +     * 更新内容状态
     * +     * @param map 包含 contentId 和 statusCode 的键值对
     * +     * @return 操作结果
     * +
     */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody Map<String, Integer> map) {
        Integer contentId = map.get("contentId");
        Integer statusCode = map.get("statusCode");

        if (contentId == null || statusCode == null) {
            return Result.error(ResultCodeEnum.PARAMETER_EMPTY);
        }

        Contents contents = new Contents();
        contents.setContentId(contentId);
        contents.setStatusCode(statusCode);

        boolean success = contentsService.updateStatus(contents);
        return success ? Result.ok() : Result.error(ResultCodeEnum.UPDATE_FAILED);
    }
}
